package org.alex.samples.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.alex.samples.model.User;
import org.alex.samples.service.ISubjectService;
import org.alex.samples.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/teacher")
@Controller
public class TeacherController {

	@Autowired
	private IUserService userService;

	@Autowired
	private ISubjectService subjectService;
	
	@ResponseBody
	@RequestMapping(value = "/getAllstudents")
	public Map<String, Object> getAllStudent(Integer subjectId) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<User> users = userService.findAllUsers();
		List<String> userId = new ArrayList<String>();
		List<String> userName = new ArrayList<String>();
		for (int i = 2; i < users.size(); i++) {
			userId.add(users.get(i).getId());
			userName.add(users.get(i).getName());
		}
		map.put("usersId", userId);
		map.put("usersName", userName);
		map.put("selectedUser", subjectService.getAllUserBySubject(subjectId));
		return map;
	}

	//保存科目相关学生
	@ResponseBody
	@RequestMapping({ "/saveSubjectStudents" })
	private Map<String, Object> saveSubjectStudents(@RequestParam(name = "subjectStudents[]") String[] subjectStudents,
			Integer subjectId) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", false);
		List<String> list = new ArrayList<String>();
		for (String student : subjectStudents) {
			list.add(student);
		}
		subjectService.saveSubjectStudent(list, subjectId);
		map.put("success", true);

		return map;
	}

	//改变考试状态
	@ResponseBody
	@RequestMapping({ "/changeStatus" })
	public Map<String, Object> changeStatus(Integer id,Integer status) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", false);
		try {
			subjectService.changeStatusById(id, status);
			map.put("status", status);
			map.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

}
